#include "System.h"


BYTE SystemState;

BOOL ButtonPressed[4];
BYTE LastButtonPressed[4];


void InitButtonDetect()
{
 ButtonPressed[0] = FALSE;
 ButtonPressed[1] = FALSE;
 ButtonPressed[2] = FALSE;
 ButtonPressed[3] = FALSE;

 LastButtonPressed[0] = 0;  
 LastButtonPressed[1] = 0;  
 LastButtonPressed[2] = 0;  
 LastButtonPressed[3] = 0;  
}

void ButtonDetect()
{
  
 if(LastButtonPressed[0] == 0 && SW1_ACTIVE>0)
  ButtonPressed[0] = TRUE;
 
 if(LastButtonPressed[1] == 0 && SW2_ACTIVE>0)
  ButtonPressed[1] = TRUE;
 
 if(LastButtonPressed[2] == 0 && SW3_ACTIVE>0)
  ButtonPressed[2] = TRUE;
 
 if(LastButtonPressed[3] == 0 && SW4_ACTIVE>0)
  ButtonPressed[3] = TRUE;
 

  LastButtonPressed[0] = SW1_ACTIVE;
  LastButtonPressed[1] = SW2_ACTIVE;
  LastButtonPressed[2] = SW3_ACTIVE;
  LastButtonPressed[3] = SW4_ACTIVE;
}

void SendSmartCarStatusToHost()
{
    if(HostUpdateTimer >= HOST_UPDATE_TIME)
    {
        HostUpdateTimer = 0;
      
        MyAncillaryMsgData.SystemState.Current = SystemState;
        (void)AssembleMessage(&OutgoingMsgBuf[0],MSG_POST_SYSTEM_STATE , &OutgoingMsgLength, &MyAncillaryMsgData)  ;
				(void)SerialByteArrayEnqueue(&SCI1OutgoingQueue,&OutgoingMsgBuf[0],OutgoingMsgLength);
      
        (void)AssembleMessage(&OutgoingMsgBuf[0],MSG_POST_SENSOR_DATA, &OutgoingMsgLength, CurrentSensorData)  ;
				(void)SerialByteArrayEnqueue(&SCI1OutgoingQueue,&OutgoingMsgBuf[0],OutgoingMsgLength);
             
        if(SystemState == SYSTEM_STATE_AUTO_NV)
        {
           (void)AssembleMessage(&OutgoingMsgBuf[0],MSG_POST_AUTO_NV_STATE_VARIABLES, &OutgoingMsgLength, &MyAutoNVStateVariables);
				   (void)SerialByteArrayEnqueue(&SCI1OutgoingQueue,&OutgoingMsgBuf[0],OutgoingMsgLength);
        }
    }

}

void InitSystemVariables()
{
    SystemState = SYSTEM_STATE_INIT;

}


void EnterSystemState(BYTE NextState)
{
   switch(NextState)
   {
      default:
      case SYSTEM_STATE_INIT:
          SystemState = NextState;
      break;
      
      case SYSTEM_STATE_IDLE:
          SystemState = NextState;
      break;
      
      case SYSTEM_STATE_AUTO_NV:
          InitAutoNV();
          SystemState = NextState;
      break;
      
   }
}

